
<template>

  <div class="add-edit-menu-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.menu_name')" prop="name">
        <el-input v-model="ruleForm.name" :placeholder="$t('tables_title.menu_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.menu_code')" prop="code">
        <el-input v-model="ruleForm.code" :placeholder="$t('tables_title.menu_code')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.parent_num')" prop="pcodeName">
        <div class="deptname-input" @click="innerVisible = true"></div>
        <el-input v-model="ruleForm.pcodeName" :placeholder="$t('tables_title.parent_num')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.menu')" prop="menuFlag">
        <el-radio-group v-model="ruleForm.menuFlag">
          <el-radio label="Y">{{$t('tables_title.yes')}}</el-radio>
          <el-radio label="N">{{$t('tables_title.no')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('tables_title.req_addr')" prop="url">
        <el-input v-model="ruleForm.url" :placeholder="$t('tables_title.req_addr')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sys_type')" prop="systemType">
        <el-radio-group v-model="ruleForm.systemType">
          <el-radio v-for="(item,index) in dictCodeList" :key="index" :label="item.code">{{item.name}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sort')" prop="sort">
        <el-input v-model="ruleForm.sort" :placeholder="$t('tables_title.sort')"></el-input>
      </el-form-item>

      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>

    <el-dialog :title="$t('tables_title.parent_num')" width="350px" :visible.sync="innerVisible" append-to-body>
        <div class="set-pname-view">
          <div class="pname-data-view">
            <el-tree  :data="treeData" :props="treeProps" node-key="id" default-expand-all :expand-on-click-node="false"
              @node-click="handleNodeClick"></el-tree>
          </div>
          <div style="text-align: center;">
            <el-button @click="innerVisible = false">{{$t('global.cancel')}}</el-button>
          </div>
        </div>
    </el-dialog>
  </div>

</template>

<script>

import { getSelectMenuTreeList,getMenuInfo,addMenu,editMenu } from '@/api/menuApi.js'

import { getDictsByCode } from '@/api/dictApi.js'

import { getMenuTreeData } from '@/common/utils/util.js'

export default {
  data(){
    return{
      loading:false,
      innerVisible:false,
      treeData:[],
      treeProps: {
        children: 'children',
        label: 'name'
      },
      dictCodeList:[],
      ruleForm:{
        name:'',
        code:'',
        pcodeName:'',
        pid:'',
        menuFlag:'Y',
        url:'',
        systemType:'',
        sort:'',
        icon:''
      },
      rules:{
          name: [
            { required: true, message: this.$t('menu_management.empty_menu_name'), trigger: 'blur' },
          ],
          code: [
            { required: true, message: this.$t('menu_management.empty_menu_code'), trigger: 'blur' },
          ],
          pcodeName: [
            { required: true, message: this.$t('menu_management.empty_parent_num'), trigger: 'blur' },
          ],
          url: [
            { required: true, message: this.$t('menu_management.empty_req_addr'), trigger: 'blur' },
          ],
          systemType: [
            { required: true, message: this.$t('menu_management.empty_sys_type'), trigger: 'blur' },
          ],
          sort: [
            { required: true, message: this.$t('global.empty_sort'), trigger: 'blur' },
          ],
      }
    }
  },
  props: ['editMenuInfo'],
  watch:{
    editMenuInfo(newVal,oldVal){
      this.ruleForm = newVal
      this.getMenuInfoById()
    }
  },
  components:{

  },
  created(){
    this.ruleForm = this.editMenuInfo
    this.getDictList()
    this.getMenuList()
    this.getMenuInfoById()
  },
  methods:{
    handleNodeClick(data){
      console.log(data)
      this.ruleForm.pid = data.id
      this.ruleForm.pcodeName = data.name
      this.innerVisible = false
    },
    getDictList(){
      let formData = new FormData()
      formData.append("dictTypeCode",'SYSTEM_TYPE')
      getDictsByCode({dictTypeCode:'SYSTEM_TYPE'}).then(res=>{
        this.dictCodeList = res.data.data
        console.log(this.dictCodeList)
      })
    },
    getMenuList(){
      getSelectMenuTreeList().then((res)=>{
        var childrenData = getMenuTreeData(res.data)
        this.treeData = res.data.filter(item=>{
          return item.id == 0
        })
        if(this.treeData.length > 0){
          this.treeData[0].children = childrenData
        }
      })
    },
    getMenuInfoById(){
      if(this.ruleForm.menuId){
        getMenuInfo({menuId:this.ruleForm.menuId}).then(res=>{
          this.ruleForm = res.data.data
        })
      }else{
        this.ruleForm.menuFlag = 'Y'
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = new FormData()
          formData.append("name",this.ruleForm.name)
          formData.append("code",this.ruleForm.code)
          formData.append("pid",this.ruleForm.pid)
          formData.append("pcodeName",this.ruleForm.pcodeName)
          formData.append("menuFlag",this.ruleForm.menuFlag)
          formData.append("url",this.ruleForm.url)
          formData.append("systemType",this.ruleForm.systemType)
          formData.append("sort",this.ruleForm.sort)
          formData.append("icon",'adasdasdsa')
          if(this.ruleForm.menuId){
            formData.append("menuId",this.ruleForm.menuId)
            this.userEditMenu(formData)
          }else{
            formData.append("menuId",'')
            this.userAddMenu(formData)
          }
        } else {
          return false;
        }
      });
    },
    userEditMenu(formData){         //修改role信息
      this.loading = true
      editMenu(formData).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{
         this.loading = false
      })
    },
    userAddMenu(formData){          //新增管Role
      this.loading = true
      addMenu(formData).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{
         this.loading = false
      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit-menu-view{
  padding: 0 30px;
}

.set-pname-view{
  width: 100%;
  height: 500px;
  display: flex;
  flex-direction: column;
  .pname-data-view{
    flex: 1;
    overflow: auto;
    margin-bottom: 20px;
    margin-top: -10px;
  }
}
</style>
